/*************************************************************************
	> File Name: main.c
	> Author: QiyangXia
	> Mail: 834061456@qq.com 
	> Created Time: 2021年12月31日 星期五 17时03分38秒
 ************************************************************************/
#include "main.h"

int main(int argc, const char **argv)
{
	system("clear");
	int max=0;
	struct myclass *pcls=NULL;

	if(argc < 2)
	{
		printf("The argument is too few,Usage:./a.out 50:\n");
		return 1;
	}
	sscanf(argv[1],"%d",&max);
    pcls=create_stuinfo_mem(max);
	mainloop(pcls);
	destroy_stuinfo_mem(pcls);
	pcls=NULL;

	return 0;
}


int mainloop(struct myclass *pcls)
{
	struct stu pt;
	int enternum=0;  
	int op=0;
	int bl=-1;
	char entername[NAME_LEN]="";
	int exitflag=0;
	while(1)
	{
		op=ui();
		switch(op)
		{
		case 1:
			system("clear");
			printf("\n\n-------------【插入】-------------\n");
			printf("请输入学号，姓名，性别，数学成绩，英语成绩，中文成绩：");
			scanf("%d%s%s%f%f%f",&pt.no,pt.name,pt.sex,&pt.math,&pt.english,&pt.chinese);
			while(getchar()!='\n'){}
			if(bl==search_no(pcls,pt.no))
			{
				insert_student(pcls,pt);
				printf("学生以成功录入系统\n");
			}else{
				printf("该学号已有学生，插入失败，请稍后重新添加\n");
			}
			break;
		case 2:
			printf("\n\n-------------【删除】-------------\n");
			printf("请输入要删除的学号:");   
			scanf("%d",&enternum);    
			while(getchar()!='\n'){}  
			del_data(pcls,search_no(pcls,enternum));
			printf("该学生信息已删除\n");
			//system("clear");
			break;
		case 3:
			printf("\n\n-------------【查询】-------------\n");
			printf("请输入要查找的学生姓名:");
			scanf("%s",entername);    
			while(getchar()!='\n'){}
			system("clear");
			search_name(pcls,entername);
			break;
		case 4:
			system("clear");
			printf("\n\n-------------【修改】-------------\n");
			change_data(pcls);
			break;
		case 5:
			system("clear");
			printf("\n\n-----------【总成绩单】-----------\n");
			insert_sort_sum(pcls);
			printdata(pcls);
			break;
		case 6:
			system("clear");
			printf("\n\n-----------【数学成绩单】----------\n");
			sel_sort_math(pcls);
			printdata(pcls);
			break;
		case 7:
			system("clear");
			printf("\n\n-----------【英语成绩单】----------\n");
			bubble_sort_english(pcls);
			printdata(pcls);
			break;
		case 8:
			system("clear");
			printf("\n\n-----------【中文成绩单】----------\n");
			bubble_sort_chinese(pcls);
			printdata(pcls);
			break;
		case 9:
			system("clear");
			printf("\n\n-------------【浏览】-------------\n");
			printdata(pcls);
			break;
		case 0:
			system("clear");
			printf("********************************************\n");
			printf("********************************************\n");
			printf("********************************************\n");
			printf("\t\t再见\n");
			printf("********************************************\n");
			printf("********************************************\n");
			printf("********************************************\n");
			exitflag=1;
			break;
		}
		if(exitflag)
		{
			break;
		}
	}
	return 0;
}
